<template>
  <div class="bottom-menu">
    <check-button :is-checked="isSelectAll" class="select-all" ></check-button>
    <span @click="checkClick">全选</span>
    <span class="total-price">合计: ¥{{totalPrice}}</span>
    <span class="buy-product" @click="calClick">去计算({{cartLength}})</span>
  </div>
</template>

<script>
  import checkButton from "@/components/content/checkButton/CheckButton";

  export default {
    name: "CartBottomBar",
    components: {
      checkButton
    },
    computed: {
      totalPrice() {
        return this.$store.state.cartList.filter(item => {
          return item.checked
        }).reduce((preValue, item) => {
          return preValue + item.price * item.count
        },0).toFixed(2)
      },
      cartLength() {
        return this.$store.state.cartList.filter(item => item.checked).length
      },
      cartList() {
        return this.$store.state.cartList
      },
      isSelectAll() {
        // 购物车没有商品的时候 不全选
        if (this.cartList.length === 0) return false
        // 1、filter
        // return !(this.cartList.filter(item => !item.checked.length))
        // 2、find
        // return !(this.cartList.find(item => !item.checked))
        // 3、普通遍历
        for (let item of this.cartList) {
          if (!item.checked) {
            return false
          }
        }
        return true
      }
    },
    methods: {
      checkClick() {
        if (this.isSelectAll) {
          this.cartList.forEach(item => item.checked = false)
        } else {
          this.cartList.forEach(item => item.checked = true)
        }
      },
      calClick() {
        if(!this.isSelectAll) {
          this.$toast.show('请选择购买的商品！', 2000)
        }
      }
    }

  }
</script>

<style scoped>
  .bottom-menu {
    width: 100%;
    height: 44px;
    background-color: #eee;
    position: fixed;
    bottom: 49px;
    left: 0;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    line-height: 44px;
    padding-left: 35px;
    box-sizing: border-box;
  }

  .bottom-menu .select-all {
    position: absolute;
    line-height: 0;
    left: 12px;
    top: 13px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }

  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  }
</style>
